#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_N 100
#define MAX_M 1000
#define MAX_CODE_LENGTH 63
#define CHAR_SET_SIZE 128 

typedef struct TrieNode {
    int isEnd;
    struct TrieNode *children[2];
} TrieNode;

TrieNode *createNode() {
    TrieNode *node = (TrieNode *)malloc(sizeof(TrieNode));
    node->isEnd = 0;
    node->children[0] = node->children[1] = NULL;
    return node;
}

int insertCode(TrieNode *root, const char *code) {
    TrieNode *current = root;
    for (int i = 0; code[i] != '\0'; i++) {
        int index = code[i] - '0';
        if (current->isEnd) {
            return 0; // 已是叶子节点，违反前缀规则
        }
        if (current->children[index] == NULL) {
            current->children[index] = createNode();
        }
        current = current->children[index];
    }
    if (current->isEnd || current->children[0] || current->children[1]) {
        return 0; // 已是其他编码的前缀
    }
    current->isEnd = 1;
    return 1;
}

void freeTrie(TrieNode *root) {
    if (!root) return;
    freeTrie(root->children[0]);
    freeTrie(root->children[1]);
    free(root);
}

int calculateOptimalLength(int *freq, int n) {
    int total = 0;
    int heap[MAX_N], heapSize = n;

    memcpy(heap, freq, n * sizeof(int));
    // 构建最小堆
    for (int i = (heapSize - 1) / 2; i >= 0; i--) {
        for (int j = i, k; (k = 2 * j + 1) < heapSize; j = k) {
            if (k + 1 < heapSize && heap[k + 1] < heap[k]) k++;
            if (heap[j] <= heap[k]) break;
            int temp = heap[j];
            heap[j] = heap[k];
            heap[k] = temp;
        }
    }
    // 合并节点
    while (heapSize > 1) {
        int min1 = heap[0];
        heap[0] = heap[--heapSize];
        for (int j = 0, k; (k = 2 * j + 1) < heapSize; j = k) {
            if (k + 1 < heapSize && heap[k + 1] < heap[k]) k++;
            if (heap[j] <= heap[k]) break;
            int temp = heap[j];
            heap[j] = heap[k];
            heap[k] = temp;
        }
        int min2 = heap[0];
        heap[0] = heap[--heapSize];
        for (int j = 0, k; (k = 2 * j + 1) < heapSize; j = k) {
            if (k + 1 < heapSize && heap[k + 1] < heap[k]) k++;
            if (heap[j] <= heap[k]) break;
            int temp = heap[j];
            heap[j] = heap[k];
            heap[k] = temp;
        }
        total += min1 + min2;
        heap[heapSize++] = min1 + min2;
    }
    return total;
}

int main() {
    int N;
    scanf("%d", &N);

    char chars[CHAR_SET_SIZE] = {0};
    int freq[CHAR_SET_SIZE] = {0};

    for (int i = 0; i < N; i++) {
        char c;
        int f;
        scanf(" %c %d", &c, &f);
        chars[i] = c;
        freq[i] = f;
    }

    int M;
    scanf("%d", &M);

    int optimalLength = calculateOptimalLength(freq, N);

    for (int m = 0; m < M; m++) {
        TrieNode *root = createNode();
        int currentLength = 0;
        int isValid = 1;

        for (int i = 0; i < N; i++) {
            char c;
            char code[MAX_CODE_LENGTH + 1];
            scanf(" %c %s", &c, code);

            // 查找字符对应频率
            int index = -1;
            for (int j = 0; j < N; j++) {
                if (chars[j] == c) {
                    index = j;
                    break;
                }
            }

            if (index == -1) {
                isValid = 0;
                continue;
            }

            currentLength += freq[index] * strlen(code);
            if (!insertCode(root, code)) {
                isValid = 0;
            }
        }

        if (isValid && currentLength == optimalLength) {
            printf("Yes\n");
        } else {
            printf("No\n");
        }

        freeTrie(root);
    }

    return 0;
}
